package {
	import com.astar.AStar;
	import com.astar.Grid;
	import com.astar.Node;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * 主视图类
	 * */
	public class MainView extends Sprite {
		//网格的大小
		private var cellSize:int = 16;
		//网格实例
		private var grid:Grid;
		//玩家人物，就是红色小球
		private var player:Player;
		//路径数组索引
		private var index:int;
		//Astar找到的路径
		private var path:Array;
		//开始按钮
		private var btnStart:Button;

		public function MainView() {
			super();
			initBtn();
			initPlayer();
			initGrid();
			setStartEnd();
		}

		/**初始化按钮*/
		private function initBtn():void {
			btnStart = new Button(50, 20, "START");
			btnStart.x = 200;
			btnStart.y = 0;
			btnStart.addEventListener(MouseEvent.CLICK, btnStartHandler);
			addChild(btnStart);
		}

		/**初始化玩家*/
		private function initPlayer():void {
			player = new Player(10);
			player.x = 10;
			player.y = 590;
			addChild(player);
		}

		/**初始化网格、节点数量*/
		private function initGrid():void {
			//本例子是30 * 30的，要多少自己调
			grid = new Grid(30, 30);
			//按题目铺设的障碍，也可以随机铺设
			for (var i:int = 0; i < 10; i++) {
				grid.setWalkable(i, i + 15, false);
			}
			for (var j:int = 10; j < 30; j++) {
				grid.setWalkable(j, j, false);
			}
			for (var k:int = 15; k < 25; k++) {
				grid.setWalkable(k, k - 15, false);
			}
			//绘画可视网格
			drawGrid();
		}

		/**画网格，为障碍填充颜色*/
		private function drawGrid():void {
			this.graphics.clear();
			for (var i:int = 0; i < grid.numCol; i++) {
				for (var j:int = 0; j < grid.numRow; j++) {
					var node:Node = grid.getNode(i, j);
					this.graphics.lineStyle(0);
					this.graphics.beginFill(getNodeColor(node));
					this.graphics.drawRect(i * cellSize, j * cellSize, cellSize, cellSize);
					this.graphics.endFill();
				}
			}
		}

		/**根据节点是否能通过来着色*/
		private function getNodeColor(node:Node):uint {
			if (node.walkable == false)
				return 0;
			return 0xffffff;
		}

		/**设置起点终点*/
		private function setStartEnd():void {
			//可自行设置，起点可以随机，终点可采用鼠标点击生成
			grid.setStartNode(0, 29);
			grid.setEndNode(29, 0);
		}

		/**开始寻路*/
		private function btnStartHandler(event:MouseEvent):void {
			//重新绘图
			drawGrid();
			//寻路
			findPath();
		}

		/**调用AStar中的寻路*/
		private function findPath():void {
			var aStar:com.astar.AStar = new AStar();
			//如果返回的是真，则说明找到路径，添加侦听绘制可视化路径
			if (aStar.findPath(grid)) {
				path = aStar.path;
				index = 0;
				addEventListener(Event.ENTER_FRAME, efHandler);
			}
		}

		/**每帧的动画处理*/
		private function efHandler(event:Event):void {
			//经过的节点
			var passedNode:Node = path[index];
			//将走过的节点填充为黄色
			this.graphics.lineStyle(0);
			this.graphics.beginFill(0xffff00);
			this.graphics.drawRect(passedNode.x * cellSize, passedNode.y * cellSize, cellSize, cellSize);

			//当前目标中心点 = 节点的行列 * 格子宽高 + 格子大小的一半
			var targetX:int = path[index].x * cellSize + cellSize / 2;
			var targetY:int = path[index].y * cellSize + cellSize / 2;
			var dx:int = targetX - player.x;
			var dy:int = targetY - player.y;
			//路径数组索引++，当索引大于等于路径数组长度时删除侦听
			index++;
			if (index >= path.length) {
				removeEventListener(Event.ENTER_FRAME, efHandler);
			}
			//玩家人物移动一格
			player.x += dx;
			player.y += dy;
		}
	}
}
